package com.devsoft.foodfrenzy;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.util.GLState;

import com.devsoft.foodfrenzy.SceneManager.SceneType;

public class TitleScene extends BaseScene{
	
	
	@Override
	public void createScene() 
	{
		UserData.getInstance().init(activity.getBaseContext());
		createBackground();
		createTouchArea();
		createAnimation();
		
		if(!UserData.getInstance().getMusicPref())
		{
			resourcesManager.ff_intro.play();
		}
	}
	
	private void createTouchArea()
	{
		final Rectangle screen = new Rectangle(400, 240, 800, 480, vbom)	//camera at center, rectangle is inverted?
        {	
            public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
            {
            	if (touchEvent.isActionUp())
                {
            		tapDone();
                }
                return true;
            };
        };
        screen.setVisible(false);
        registerTouchArea(screen);
        attachChild(screen);
    	
	}
	
	private void createAnimation()
	{
		AnimatedSprite tap_title = new AnimatedSprite(400, 48, resourcesManager.tap_animation_region, vbom);
		tap_title.animate(450);
		attachChild(tap_title);
	}
	
	private void tapDone()
	{
		SceneManager.getInstance().createMenuScene();
		SceneManager.getInstance().loadMenuScene(engine);
	}
	
	private void createBackground()
	{
	    attachChild(new Sprite(400, 240, resourcesManager.title_background_region, vbom)
	    {
	        @Override
	        protected void preDraw(GLState pGLState, Camera pCamera) //makes picture quality smoother
	        {
	            super.preDraw(pGLState, pCamera);
	            pGLState.enableDither();
	            
	        }
	        
	    });
	    
	}

	@Override
	public void onBackKeyPressed() 	//exit app when backed
	{
		System.exit(0);
	}
	

	@Override
	public SceneType getSceneType()
	{
	    return SceneType.SCENE_TITLE;
	}
	
	
	@Override
	public void disposeScene() 
	{
		this.detachSelf();
        this.dispose();
	}
	
}